package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.bean.CollectionListBean;
import com.elinkway.infinitemovies.bean.Favorite;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaojian on 16-3-14.
 */
public class FavoriteListParser extends LVideoMobileParser<CollectionListBean> {
    private JSONObject arr;
    private CollectionListBean mCollectionListBean = new CollectionListBean();
    private List<Favorite> collectionList = new ArrayList();

    @Override
    public CollectionListBean parse(JSONObject data) throws Exception {
        if(!"200".equals(super.getCode())) {
            return null;
        }else {
            mCollectionListBean.setTotal(arr.optInt("total"));
            JSONArray arrayList = arr.getJSONArray("data");
            for(int i = 0; i < arrayList.length(); i++) {
                Favorite mFavorite = new Favorite();
                mFavorite.setAid(((JSONObject) arrayList.get(i)).optString("aid"));
                mFavorite.setVt(((JSONObject) arrayList.get(i)).optString("vt"));
                mFavorite.setSrc(((JSONObject) arrayList.get(i)).optString("src"));
                mFavorite.setIsend(((JSONObject) arrayList.get(i)).optString("isend"));
                mFavorite.setName(((JSONObject) arrayList.get(i)).optString("name"));
                mFavorite.setNowEpisode(((JSONObject) arrayList.get(i)).optString("nowepisodes"));
                mFavorite.setEpisodes(((JSONObject) arrayList.get(i)).optString("episodes"));
                mFavorite.setPoster(((JSONObject) arrayList.get(i)).optString("poster"));
                collectionList .add(mFavorite);
            }
            mCollectionListBean.setCollectionList(collectionList);
            return mCollectionListBean;
        }
    }

    @Override
    protected JSONObject getData(String data) throws JSONException {
        arr = new JSONObject(data);
        data = "{\"data\":\"data\"}";
        return super.getData(data);
    }
}
